import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Game {
    public String[] suits = {"♥","♠","♦","♣"};
    public List<Card> cardsList = new ArrayList<>();//作为扑克牌的容器
    //创建一副扑克牌
    public List<Card> createCards() {
        for (int i = 0; i < suits.length; i++) {
            for (int j = 1; j <= 13 ; j++) {
                Card card = new Card(suits[i],j);
                cardsList.add(card);
            }
        }
        return cardsList;
    }

    //洗牌操作

    public void shuffle(List<Card> cardsList) {
        Random random = new Random();
        for (int i = 51; i > 0; i--) {
            int randIndex = random.nextInt(i);//生成 [0,i-1] 区间的随机数
            //i下标位置 与 randIndex下标位置元素进行交换
            swap(i,randIndex);
        }
    }
    //i下标位置 与 randIndex下标位置元素进行交换
    private void swap(int i,int randIndex) {
        Card tmp = cardsList.get(i);
        cardsList.set(i,cardsList.get(randIndex));
        cardsList.set(randIndex,tmp);
    }
    //发牌，三个人轮流取牌，一次取一张，最终每个人五张牌
    public List<List<Card>> play(List<Card> cardsList) {
        List<List<Card>> cardLists = new ArrayList<>();
        List<Card> hand0 = new ArrayList<>();
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        cardLists.add(hand0);
        cardLists.add(hand1);
        cardLists.add(hand2);
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Card card = cardsList.remove(0);
                cardLists.get(j).add(card);
            }
        }
        return cardLists;
    }
}
